//引入
const express = require('express');
const db = require('../../utils/db')

//配置路由器
const salary_HYH = express.Router();

//接口编写
//获取
salary_HYH.get('/salary/query', (req, res) => {
    const page = req.query.page;
    const count = req.query.count;
    const sql = `select * from salary_project limit ${(page - 1) * count},${count}`;
    const sql2 = `select count(*) as total from salary_project;`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2);
                    res.send({ error: 1, msg: err2 });
                } else {
                    console.log(data2);
                    res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total });
                }
            })
        }
    })
})

// 删除
salary_HYH.post('/salary/del', (req, res) => {
    const sql = 'delete from salary_project where p_id="' + req.body.id + '";';
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '删除成功' })
        }
    })
})

//查询搜索
salary_HYH.post('/salary/sousuo', (req, res) => {
    const number = req.body.number;
    const name = req.body.name;
    const type = req.body.type;
    const state = req.body.state;

    const sql = `SELECT * FROM salary_project WHERE p_number='${number}' OR p_project='${name}' OR p_type='${type}' OR p_state='${state}';`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '查询成功', data: data })
        }
    })
})

//添加
salary_HYH.post('/salary/add', (req, res) => {
    const sql = `INSERT INTO salary_project VALUES(NULL,'${req.body.number}','${req.body.project}','${req.body.state}','${req.body.type}','${req.body.data}','${req.body.length}','${req.body.precision}','${req.body.note}','${req.body.founder}','${req.body.founderTime}','${req.body.updata}','${req.body.updataTime}');`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: "添加成功", data: data })
        }
    })
})

//修改
salary_HYH.post('/salary/modify', (req, res) => {
    const sql = `update salary_project set p_project='${req.body.project}',p_state='${req.body.state}',p_type='${req.body.type}',p_data='${req.body.data}',p_length='${req.body.length}',p_precision='${req.body.precision}',p_note='${req.body.note}',p_founder='${req.body.founder}',p_founderTime='${req.body.founderTime}',p_update='${req.body.updata}',p_updateTime='${req.body.updataTime}' where p_number='${req.body.number}';`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '修改成功', data: data })
        }
    })
})

//导出
module.exports = salary_HYH;